//
//  BIZConsultHistoryViewController.m
//  biz
//
//  Created by 边文辉 on 2017/3/18.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZConsultHistoryViewController.h"
#import "BIZConsultQuestionCell.h"
#import "BIZConsultQModel.h"
#import "BIZConsultDetailTableViewController.h"
static NSString *const QuesetionCellID = @"BIZConsultQuestionCellID";

@interface BIZConsultHistoryViewController ()


@property (nonatomic, assign) int last;
@property (nonatomic, copy) NSMutableArray *list;

@end

@implementation BIZConsultHistoryViewController


- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"历史记录";
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([BIZConsultQuestionCell class]) bundle:nil] forCellReuseIdentifier:QuesetionCellID];
    
    self.tableView.estimatedRowHeight = 200 ;
    
    self.tableView.backgroundColor = HEXCOLOR(0xEEEDEE);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [UIView new];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNew)];
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadHistorys)];
    
    
    [self loadNew];
}

- (void)loadNew
{
    self.last = 1;
    [self.list removeAllObjects];
    [self loadHistorys];
}



- (void)loadHistorys
{
    [MBProgressHUD showAnimationHUD];
    [RequestTool POST:kConsultO2OHistoryUrl
           parameters:@{@"pageIndex" : @(_last), @"pageEnd" : @(_last + 9)}
             progress:nil
              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                  [self stopRefresh];
        
                  int code = [responseObject[@"code"] intValue];
                  if (code == 1) {
                      [MBProgressHUD hideHUD];
                      BIZConsultQRetModel *model = [BIZConsultQRetModel yy_modelWithJSON:responseObject];
                      [self.list addObjectsFromArray:model.data];
                      _last += model.data.count;
                      
                      [self.tableView reloadData];
                      
                  } else if (code == 2) {
                      BIZLoginViewController *vc = [BIZLoginViewController viewControllerFromStoryboard];
                      MPWeakSelf(self);
                      vc.loginSuccessBlock = ^(){
                          [weakself loadNew];
                      };
                      [self presentViewController:vc animated:YES completion:nil];
                      
                  } else {
                          [MBProgressHUD showErrorMessage:responseObject[@"des"]];
                  }
                  
              }
              failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                  [self stopRefresh];
                  if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
                      [MBProgressHUD showErrorMessage:@"网络不给力"];
                  }
                  else {
                      [MBProgressHUD showErrorMessage:@"服务器繁忙"];
                  }

              }
     ];
}

- (void)stopRefresh
{
    !self.tableView.mj_header.isRefreshing ? : self.tableView.mj_header.endRefreshing;
    !self.tableView.mj_footer.isRefreshing ? : self.tableView.mj_footer.endRefreshing;
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BIZConsultQuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:QuesetionCellID forIndexPath:indexPath];
    
    cell.model = self.list[indexPath.row];
    cell.showImage = BIZConsultQuestionCellShowImageTypeNone;
    cell.isHistoryList = YES;
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}




#pragma mark - Table View Delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    BIZConsultDetailTableViewController *detail = [[BIZConsultDetailTableViewController alloc] init];
    BIZConsultQModel *model = self.list[indexPath.row];
    detail.qModel = model;
    detail.needLoadAnswer = YES;
    
    MPWeakSelf(self);
    detail.checkDetailBlock = ^(){
        model.status = @"1";
        [weakself.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationFade];
    };
    [self.navigationController pushViewController:detail animated:YES];

}



- (NSMutableArray *)list
{
    if (!_list) {
        _list = [NSMutableArray array];
    }
    return _list;
}


@end
